package com.gscitysfy.cus.drain.utils;

import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @Author jiang_wen
 * @Date 2022/8/25 15:42
 * @Description 两坐标间距测量
 */
public class CoordinateUtil {

    /**
     * 两经纬度坐标距离计算
     *
     * @param startXPoint
     * @param startYPoint
     * @param endXPoint
     * @param endYPoint
     * @return
     */
    public static BigDecimal getDistance(String startXPoint, String startYPoint, String endXPoint, String endYPoint) {
        double startXPointDouble = Double.parseDouble(startXPoint);
        double startYPointDouble = Double.parseDouble(startYPoint);
        double endXPointDouble = Double.parseDouble(endXPoint);
        double endYPointDouble = Double.parseDouble(endYPoint);
        GlobalCoordinates source = new GlobalCoordinates(startXPointDouble, startYPointDouble);
        GlobalCoordinates target = new GlobalCoordinates(endXPointDouble, endYPointDouble);
        double distanceMeter = getDistanceMeter(source, target, Ellipsoid.WGS84);
        BigDecimal bigDecimal = new BigDecimal(distanceMeter);
        return bigDecimal.setScale(3, RoundingMode.HALF_UP);
    }

    /**
     * 两经纬度坐标距离计算
     *
     * @param startXPoint
     * @param startYPoint
     * @param endXPoint
     * @param endYPoint
     * @return
     */
    public static BigDecimal getDistance(BigDecimal startXPoint, BigDecimal startYPoint, BigDecimal endXPoint, BigDecimal endYPoint) {
        double startXPointDouble = startXPoint.doubleValue();
        double startYPointDouble = startYPoint.doubleValue();
        double endXPointDouble = endXPoint.doubleValue();
        double endYPointDouble = endYPoint.doubleValue();
        GlobalCoordinates source = new GlobalCoordinates(startXPointDouble, startYPointDouble);
        GlobalCoordinates target = new GlobalCoordinates(endXPointDouble, endYPointDouble);
        double distanceMeter = getDistanceMeter(source, target, Ellipsoid.WGS84);
        BigDecimal bigDecimal = new BigDecimal(distanceMeter / 1000);
        return bigDecimal.setScale(2, RoundingMode.HALF_UP);
    }

    public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid) {
        //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
        return geoCurve.getEllipsoidalDistance();
    }

}
